package com.ys.web.ws.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.ys.web.pojo.CompanyInfo;
import com.ys.web.ws.service.ManageCompanyInfoService;
import com.ys.web.ws.service.RemoteSEOService;

public class RemoteSEOServiceImpl implements RemoteSEOService{

	@Resource
	private ManageCompanyInfoService manageCompanyInfoService;
	@Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	
	@Override
	public String getSearchKeyword(String industryId, String mainBussiness, Integer limitNum) {
		// TODO Auto-generated method stub
		List<CompanyInfo> list = manageCompanyInfoService.getCompanyByIndustry_ws(industryId);
		StringBuffer comId = new StringBuffer();
		if(list != null && !list.isEmpty()){
			for(CompanyInfo c : list){
				comId.append(c.getId()+"").append(",");
			}
		    comId.deleteCharAt(comId.length()-1);
		}
		
		StringBuffer sql = new StringBuffer();
		sql.append("select DISTINCT keyword_search from origin_baidu where keyword_search like '%"+mainBussiness+"%' ");
		sql.append(" and DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= DATE(promotion_date) ");
		if(comId.length() != 0){
			sql.append(" and company_id in(").append(comId.toString()).append(") ");
		}
		sql.append(" order by number_exhibit desc ");
		if(limitNum != null){
			sql.append(" limit "+ limitNum +"");
		}
		List<Map<String, Object>> map = jdbcTemplate.queryForList(sql.toString());
		StringBuffer searchKeyword = new StringBuffer();
		if(map!=null && !map.isEmpty()){
			for(int i = 0; i<map.size(); i++ ){
				searchKeyword.append(map.get(i).get("keyword_search")).append(",");
			}
			searchKeyword.deleteCharAt(searchKeyword.length()-1);
		}
		if(searchKeyword.length() != 0){
			return searchKeyword.toString();
		}else{
			return null;
		}
	}

}
